package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcAttendance;
import com.csun.cmny.provider.model.dto.AttendanceQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcAttendanceService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.apache.poi.hssf.usermodel.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/excel")
@Api(value = "Web-ApiRestController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ExcelController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcAttendanceService attendanceService;



    @PostMapping(consumes = "multipart/form-data", value = "/save")
    @ApiOperation(httpMethod = "POST", value = "上传考勤")
    public Wrapper save (HttpServletRequest request) throws IOException, ParseException {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;



        MultipartFile file = multipartRequest.getFile("file");



        InputStream inputStream = file.getInputStream();

        HSSFWorkbook workbook=null;

        try {

             workbook = new HSSFWorkbook(file.getInputStream());
        }catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("excel.error", null));
        };

        List<OmcAttendance> omcAttendances = new ArrayList<>();


        for (int j = 0; j < workbook.getNumberOfSheets(); j++) {

            HSSFSheet st = workbook.getSheetAt(j);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");




            for (int i = 1; i < st.getPhysicalNumberOfRows(); i++) {

                if (st.getRow(i).getRowNum()<1){
                    continue;
                }


                OmcAttendance omcAttendance = new OmcAttendance();
                st.getRow(i).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                st.getRow(i).getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                st.getRow(i).getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                st.getRow(i).getCell(3).setCellType(Cell.CELL_TYPE_STRING);

                if(PublicUtil.isEmpty(st.getRow(i).getCell(0).getStringCellValue())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("attendance.excel.name.null", null));
                }
                if(PublicUtil.isEmpty(st.getRow(i).getCell(1).getStringCellValue())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("attendance.excel.AttendanceTime.null", null));
                }
                if(PublicUtil.isEmpty(st.getRow(i).getCell(2).getStringCellValue())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("attendance.excel.ClosingTime.null", null));
                }
                if(PublicUtil.isEmpty(st.getRow(i).getCell(3).getStringCellValue())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("attendance.CellValue.name.null", null));
                }

                if(PublicUtil.isNotEmpty(st.getRow(i).getCell(4))){
                    st.getRow(i).getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                    omcAttendance.setRemark(st.getRow(i).getCell(4).getStringCellValue());
                }

                omcAttendance.setStaffName(st.getRow(i).getCell(0).getStringCellValue());
                omcAttendance.setAttendanceTime(st.getRow(i).getCell(1).getStringCellValue());
                omcAttendance.setClosingTime(st.getRow(i).getCell(2).getStringCellValue());
                String stringCellValue = st.getRow(i).getCell(3).getStringCellValue();

                try {

                    Date parse = simpleDateFormat.parse(stringCellValue);
                    omcAttendance.setTheDate(parse);
                }catch (Exception e) {
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("attendance.date.error", null));
                };
                omcAttendance.setOperator(getLoginAuthDto().getUserName());
                omcAttendance.setGroupId(getLoginAuthDto().getGroupId());
                omcAttendance.setUserId(getLoginAuthDto().getUserId());


                omcAttendances.add(omcAttendance);

            }




        }


        if(PublicUtil.isNotEmpty(omcAttendances)){
            int  save=attendanceService.saves(omcAttendances);

            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
        }else {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("excel.size.error", null));
        }




    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询考勤记录")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper selectAttendance(@RequestBody AttendanceQueryDto attendanceQueryDto) {

        attendanceQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<OmcAttendance>list=attendanceService.selectAttendance(attendanceQueryDto);
        PageInfo<OmcAttendance> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

}
